package cc.stonemarket.caricature.action;

import android.app.TabActivity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;

public class MainActivity extends TabActivity implements OnTabChangeListener {

	private TabHost myTabhost;
	protected int myMenuSettingTag=0;
	protected Menu myMenu;
	private static final int myMenuResources[] = { R.menu.favorite_menu,R.menu.search_menu, R.menu.settings_menu};
	
	private ListView bookListView;
	
	
	
	
	/**Activity初始化*/
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		//各个Tab自身布局处理
		this.setTabhost();
		this.setTab1();
		this.setTab2();
		this.setTab3();
	}
	
	/**点TAB触发的事件*/
	public void onTabChanged(String tagString) {
		// myMenuSettingTag = Integer.parseInt(tagString);
		if (tagString.equals("Tab1")) myMenuSettingTag = 1;
		else if (tagString.equals("Tab2")) myMenuSettingTag = 2;
		else if (tagString.equals("Tab3")) myMenuSettingTag = 3;
		//每进入不同的Tab,都改变菜单
		if (myMenu != null) onCreateOptionsMenu(myMenu);
	}
	
	/**创建各个TAB的MENU菜单*/
	public boolean onCreateOptionsMenu(Menu menu) {
		myMenu = menu;
		//清空MENU菜单
		myMenu.clear();
		//从TabActivity这里获取一个MENU过滤器
		MenuInflater inflater = getMenuInflater();
		//动态加入数组中对应的XML MENU菜单
		switch (myMenuSettingTag) {
			case  1: inflater.inflate(myMenuResources[0], menu); break;
			case  2: inflater.inflate(myMenuResources[1], menu); break;
			case  3: inflater.inflate(myMenuResources[2], menu); break;
			default: inflater.inflate(myMenuResources[0], menu); break;
		}
		return super.onCreateOptionsMenu(menu);
	}
	
//=================================onCreate方法初始化用=======================================
	
	/**初始化主界面各Tab的布局、标题、图标等*/
	private void setTabhost(){
		//获取容器,其实就是调用====>this.setContentView(com.android.internal.R.layout.tab_content);
		myTabhost=this.getTabHost();
		LayoutInflater.from(this).inflate(R.layout.main, myTabhost.getTabContentView(), true);
		myTabhost.setBackgroundColor(Color.argb(150, 22, 70, 150));
		//myTabhost.setBackgroundResource(R.drawable.back_green);
		//创建第1个Tab
		TabSpec tab=myTabhost.newTabSpec("Tab1");
		tab.setIndicator(getString(R.string.favorites), getResources().getDrawable(R.drawable.favorite));//设置Title and Icon
		tab.setContent(R.id.main_tab1);//设置layout
		myTabhost.addTab(tab);//添加到容器
		//第2个Tab
		tab=myTabhost.newTabSpec("Tab2");
		tab.setIndicator(getString(R.string.search), getResources().getDrawable(R.drawable.search));
		tab.setContent(R.id.main_tab2);
		myTabhost.addTab(tab);
		//第3个Tab
		tab=myTabhost.newTabSpec("Tab3");
		tab.setIndicator(getString(R.string.settings), getResources().getDrawable(R.drawable.settings));
		tab.setContent(R.id.main_tab3);
		myTabhost.addTab(tab);
		//监听Tab的事件
		myTabhost.setOnTabChangedListener(this);
		
		//控制TabHost中标签Tab的高度和宽度
		final TabWidget tabWidget = myTabhost.getTabWidget();
		for (int i =0; i < tabWidget.getChildCount(); i++) {
			View tempTab = tabWidget.getChildAt(i);
			tempTab.getLayoutParams().height = 60;
			//tempTab.getLayoutParams().width = 65;
			//((TextView)tempTab.findViewById(android.R.id.title)).setTextSize(10);
	        //((ImageView)tempTab.findViewById(android.R.id.icon)).setPadding(0, 0, 0, 0);//左、上、右、下
		}
	}
	
	private void setTab1(){
      bookListView = (ListView) findViewById(R.id.booklistview);
      bookListView.setEmptyView(findViewById(R.id.empty));
      bookListView.setTextFilterEnabled(true);
      bookListView.setOnItemClickListener(new OnItemClickListener() {
          public void onItemClick(final AdapterView<?> parent, final View v, final int index, final long id) {
        	  //TODO 点击某本书，跳到相应详细页面
        	  
          }
       });
	}
	
	private void setTab2(){
		
	}
	
	private void setTab3(){
		TextView text=(TextView)findViewById(R.id.tab3text);
		text.setText("跳到ShowLayout界面");
		text.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				//Toast.makeText(MainActivity.this, "TextView被点击", Toast.LENGTH_LONG).show(); 
				Intent forward = new Intent(MainActivity.this,ShowLayout.class);
				startActivity(forward); 
			}
		});
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}

